package com.huazai.juc.cec.thread.july.eighteen;

import com.huazai.juc.utils.SleepUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 本例子用于展示多线程的资源共享问题。
 */
@Slf4j
public class SharedResourceSecurity {
//    private static int security = 0;

    public static void main(String[] args) {


        AtomicInteger security = new AtomicInteger();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i ++) {
                security.getAndIncrement();
            }
        }, "t1");
        t1.start();

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i ++) {
                security.getAndDecrement();
            }
        }, "t2");
        t2.start();

        SleepUtil.MySleep(2);

        log.info("security is {}", security);
    }
}
